package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
//    执行查询
        List<Emp> empList =  empMapper.list(name, gender, begin, end);
        // 获取 Page<Emp> 对象
//    Page<Emp> p = (Page<Emp>) empList;
        Page<Emp> p = (Page<Emp>) empList;
//封装pageBean
        PageBean pageBean  = new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

/*
* 删除
* */
    @Override
    public void deletes(List<Integer> ids) {
    empMapper.deletes(ids);
    }


    /*
    * 新增
    * */
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.inster(emp);
    }

    @Override
    public Emp getByid(Integer id) {
     return empMapper.getByid(id);
    }

    @Override
    public void selet(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.selet(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByusername(emp);
    }


//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //      获取总记录数
//        Long count = empMapper.count();
////        获取分页查询的结果
//        Integer start = (page - 1 ) * pageSize;
//        List<Emp> empList = empMapper.page(start,pageSize);
//
//        PageBean pageBean  = new PageBean(count,empList);
//
//        return pageBean;
//    }


}
